<template>
  <div class="data">
    <div class="banner">
      <img src="../../assets/banner3.png" alt="" />
    </div>
    <div style="margin: 50px 0 100px 0">
      <common-data />
    </div>
    <!-- //:action:'' :auto-upload="false" 不传给后端 accept=".xlsx"限制格式 
//:on-change="onChange"文件状态改变时触发事件 -->
<el-upload :action='ture' accept=".xlsx" :on-change="onChange" :auto-upload="ture"></el-upload>
  </div>
</template>

<script>
import * as xlsx from 'xlsx'
import CommonData from "../../components/CommonData.vue";
export default {
  name: "Data",
  components: { CommonData },
  methods:{
	 upload(file){
    	return new Promise(resolve=>{
        	let reader = new FileReader()
        	reader.readAsBinaryString(file);
        	reader.onload = ev=>{
            	resolve(ev.target.result)
        	}
    	})
	},
	async onChange(ev){
		var testmsg=ev.name.substring(ev.name.lastIndexOf('.')+1)
        //const extension = testmsg === 'xls'
        const extension2 = testmsg === 'xlsx'
        // const isLt2M = ev.size / 1024 / 1024 < 10
        if(!extension2) {
        	this.$message({
            	message: '上传文件只能是 xls、xlsx格式!',
                type: 'warning'
            });
         }
         // if(!isLt2M) {
         //     this.$message({
         //         message: '上传文件大小不能超过 10MB!',
         //         type: 'warning'
         //     });
         // }
         if(extension2) {
        	let file = ev.raw
      		if (!file) return
      		let reader = await this.upload(file)
      		const worker = xlsx.read(reader, { type: 'binary' })
      		console.log(worker)
      		// 这个是将数据进行一步拼接
      		//  worker.Sheets[worker.SheetNames[0]]
      		// 将返回的数据转换为json对象的数据
       		reader = xlsx.utils.sheet_to_json( worker.Sheets[worker.SheetNames[0]])
       		console.log(reader);
           alert('上传成功')
         }
	}
}       
  
};
</script>

<style lang='scss' scoped>
.data {
  width: 100%;

  .banner {
    width: 100%;
    height: 30%;
    img {
      width: 100%;
      height: 400px;
    }
  }
}
</style>